package com.happly.happlycloud.controller.test;

import com.happly.happlycloud.common.ResponseCode;
import com.happly.happlycloud.common.ServerResponse;
import com.happly.happlycloud.pojo.*;
import com.happly.happlycloud.service.IPaperService;
import com.happly.happlycloud.service.QuestionService;
import com.happly.happlycloud.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/question/")
@Api("试题信息")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionUtil questionUtil;

    @Autowired
    private IPaperService ipaperService;


    //存放获取到的当前试卷的试题，试题中包含答案
    private List<Question> questionList=null;
    //声明局部线程
    private static ThreadLocal<List<Question>> answerList=new ThreadLocal<List<Question>>();

    @RequestMapping(value = "findQuestion.do")
    @ApiOperation("添加")
    public ServerResponse<Map<String,Object>> findQuestion(Integer stateId, HttpServletRequest httpServletRequest){
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userJsonStr,User.class);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要强制登录status=10");
        }
        List<Question> list=questionService.findQuestion(stateId);
        questionList=list;
        answerList.set(list);

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("list",list);
        map.put("title",questionUtil.createPaperTitle(stateId,user.getUsername()));
        return ServerResponse.createBySuccess(map);
    }
    @RequestMapping(value = "savePaper.do")
    @ApiOperation("添加")
    //HttpServletRequest request,
    public ServerResponse<String> savePaper(HttpServletRequest httpServletRequest,String paperName){
        //data=4-C,2-C,5-B,1-B,3-A&paperTitle=zyy16117306980121
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userJsonStr,User.class);
        //1、创建试卷主表对象
        Paper paper=new Paper();
        //当前时间
        paper.setCommitTime(DateTimeUtil.getCurrentDateTime());
        paper.setPaperTitle(paperName);
        //登录用户
        paper.setUserName(user.getUsername());
        //试卷里的总分，在业务逻辑层进行判定、计算
        //2、试卷从表对象集合
        List<PaperDetail> list=new ArrayList<PaperDetail>();
        String userAsnwer=httpServletRequest.getParameter("userData");
        //4-C,2-C,5-B,1-B,3-A
        String[] aryAnswer=userAsnwer.split(",");
        System.out.println(answerList);
        for(int i=0; i<aryAnswer.length; i++){
            PaperDetail paperDetail=new PaperDetail();
            if(aryAnswer[i].charAt(1) == 'e') {
                paperDetail.setQuestionId(Integer.parseInt(aryAnswer[i].substring(0,1)));
                paperDetail.setUserAnswer(String.valueOf(aryAnswer[i].substring(2,3).charAt(0)));
                list.add(paperDetail);
            }
            if(aryAnswer[i].charAt(2) == 'e') {
                paperDetail.setQuestionId(Integer.parseInt(aryAnswer[i].substring(0,2)));
                paperDetail.setUserAnswer(String.valueOf(aryAnswer[i].substring(3,4).charAt(0)));
                list.add(paperDetail);
            }
        }

        //调用业务逻辑
        int result=questionService.savePaper(paper,list,questionList);
        if(result!=-1){
            //String.valueOf(result)
            return ServerResponse.createBySuccess(String.valueOf(result));
        }else{
            return ServerResponse.createByErrorMessage("提交试卷失败！");
        }
    }
    @RequestMapping(value = "findGrade.do")
    @ApiOperation("添加")
    public ServerResponse findGrade(HttpServletRequest httpServletRequest, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
        }
        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userJsonStr,User.class);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要强制登录status=10");
        }
        return ipaperService.findGrade(user.getUsername(),pageNum,pageSize);
    }
}
